import type { CACHE_KEY } from "@/enums/cacheEnum"

export function getCache(key: CACHE_KEY) {
  const data = localStorage.getItem(key)
  if (data) {
    const response = JSON.parse(data)
    if (response.expire_time > Date.now()) {
      return response.data
    } else {
      localStorage.removeItem(key)
      return false
    }
  }
  return false
}

export function setCache(key: CACHE_KEY, value: any, expire_time: number) {
  localStorage.setItem(
    key,
    JSON.stringify({
      data: value,
      expire_time: Date.now() + expire_time
    })
  )
}

export function removeCache(key: CACHE_KEY) {
  localStorage.removeItem(key)
}
